package br.ucb.antares.conversor;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import br.ucb.antares.HIB.FornecedorHIB;
import br.ucb.antares.entidade.Fornecedor;

@FacesConverter( value="FornecedorConverter" )
public class FornecedorConverter {
	
	public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
		if (value == null)
			return null;
		try {
			return new FornecedorHIB().consultar(Long.parseLong(value));
		} catch (Exception e) {
			e.printStackTrace();
			return new Object();
		}
	}

	public String getAsString(FacesContext context, UIComponent component, Object object) throws ConverterException {
		if (object == null)
			return null;
		Fornecedor fornecedor = (Fornecedor) object;
		
		return String.valueOf(fornecedor.getId());
	}
}
